package com.msb.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.msb.mall.product.entity.AttrEntity;
import com.msb.mall.product.service.AttrAttrgroupRelationService;
import com.msb.mall.product.service.AttrService;
import com.msb.mall.product.service.CategoryService;
import com.msb.mall.product.vo.AttrGroupRelationVO;
import com.msb.mall.product.vo.AttrGroupWithAttrsVo;
import com.msb.mall.product.vo.AttrResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.msb.mall.product.entity.AttrGroupEntity;
import com.msb.mall.product.service.AttrGroupService;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.R;



/**
 * 属性分组
 *
 * @author xxf
 * @email 1983752950@qq.com
 * @date 2022-09-10 22:42:29
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表,根据查询条件把table中的数据展示到页面
     *
     page:1, // 当前页
     limit:10, // 每页显示的条数
     sidx:"id", // 排序的字段
     order:"asc/desc", // 排序的方式
     key:"小米" // 查询的关键字
     *
     *
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params
            ,@PathVariable("catelogId") Long catelogId){
        // PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
   // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        //根据属性组id获取AttrGroupEntity对象
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        //通过AttrGroupEntity对象，获取属性组关联的三级分类的id
        Long catelogId = attrGroup.getCatelogId();

        //在三级分类中创建一个方法
        Long[] paths=categoryService.findCatelogPath(catelogId);

            attrGroup.setCatelogPath(paths);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    //http://localhost:8070/app/product/attrgroup/228/attr/relation
    @GetMapping("/{attrgroupid}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupid")Long attrgroupid){

        List<AttrEntity> list =attrService.getRelationAttr(attrgroupid);

    return R.ok().put("data",list);
    }

    //http://localhost:8070/app/product/attrgroup/attr/relation/delete
    @PostMapping("attr/relation/delete")
    public R relationDelete(@RequestBody AttrGroupRelationVO[] attrResponseVo){

        attrService.deleteRelation(attrResponseVo);

        return R.ok();
    }


    //http://localhost:8070/app/product/attrgroup/6/noattr/relation
    @GetMapping("/{attrgroupid}/noattr/relation")
    public R attrNorelation(@PathVariable("attrgroupid")Long attrgroupid
            ,@RequestParam Map<String,Object> param){


        PageUtils page=attrService.getNoAttrRelation(attrgroupid,param);

        return R.ok().put("page",page );
    }


    //http://localhost:8070/app/product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R savaBatch(@RequestBody List<AttrGroupRelationVO> vo){

        attrAttrgroupRelationService.saveBatch(vo);

        return R.ok();
    }

    //http://localhost:8070/app/product/attrgroup/231/withattr?t=1664765571820
    @GetMapping("/{catelogId}/withattr")
    public R getAttrgroupwithAttrsVO(@PathVariable("catelogId")Long catelogId){

        List<AttrGroupWithAttrsVo> list
                =attrGroupService.getAttrgroupwithAttrsByCatelogId(catelogId);

        return R.ok().put("data",list);
    }



}
